<template>
  <div class="my-image">
    <!-- 图片按钮 -->
    <div class="btn-img" @click="openDiglog">
      <img :src="value || perviewImg" alt />
    </div>
    <!-- 对话框按钮 -->
    <el-dialog :visible.sync="dialogVisible" width="720px">
      <el-tabs v-model="activeName" type="card">
        <el-tab-pane label="素材库" name="images">
          <!-- 按钮 -->
          <el-radio-group v-model="reqParams.collect" size="small" @change="toggleList()">
            <el-radio-button :label="false">全部</el-radio-button>
            <el-radio-button :label="true">收藏</el-radio-button>
          </el-radio-group>
          <!-- 图片列表 -->
          <div class="list-img">
            <div
              :class="{selected:selectImgUrl===item.url}"
              @click="selectImg(item.url)"
              class="img-item"
              v-for="item in images"
              :key="item.id"
            >
              <img :src="item.url" alt />
            </div>
          </div>
          <!-- 分页 -->
          <el-pagination
            :total="total"
            :page-size="reqParams.per_page"
            :current-change="reqParams.page"
            @current-change="changepage"
            background
            layout="prev, pager, next"
          ></el-pagination>
        </el-tab-pane>
        <!-- 上传图片 -->
        <el-tab-pane label="上传图片" name="upload">
          <el-upload
            class="avatar-uploader"
            action="http://ttapi.research.itcast.cn/mp/v1_0/user/images"
            :show-file-list="false"
            name="image"
            :on-success="uploadSuccess"
            :headers="headers"
          >
            <img v-if="uploadUrl" :src="uploadUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-tab-pane>
      </el-tabs>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmImg">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import auth from "@/utils/auth";
import img from "../assets/default.png";
export default {
  name: "my-image",
  props: ["value"],
  data() {
    return {
      //控制对话框的显示与隐藏
      dialogVisible: false,
      //当激活选择卡
      activeName: "images",
      reqParams: {
        collect: false,
        page: 1,
        per_page: 8,
      },
      images: [],
      // 总条数
      total: 0,
      //获取图片的url
      selectImgUrl: null,
      //上传时图片的地址
      uploadUrl: null,
      //设置请求头
      headers: { Authorization: `Bearer ${auth.getUser().token}` },
      perviewImg: img,
    };
  },
  //页面渲染的时候就执行
  created() {},
  methods: {
    //确认按钮
    confirmImg() {
      if (this.activeName === "images") {
        if (!this.selectImgUrl) {
          //素材框判断是否选中图片
          return this.$message.warning("请选择一张图片");
        } else {
          // this.perviewImg = this.selectImgUrl;
          this.$emit("input", this.selectImgUrl);
        }
      } else {
        //判断有上传的图片
        if (!this.uploadUrl) return this.$message.warning("请上传一张图片");
        // this.perviewImg = this.uploadUrl;
        this.$emit("input", this.uploadUrl);
      }
      //退出对话框
      this.dialogVisible = false;
      //在这个位置要做其他组件的业务
      this.$emit("confirm");
    },
    //上传图片
    uploadSuccess(response) {
      //浏览图片
      console.log(response);
      this.uploadUrl = response.data.url;
    },
    //获取当前图片地址
    selectImg(url) {
      this.selectImgUrl = url;
    },
    //当前页码传给服务器
    changepage(newpage) {
      this.reqParams.page = newpage;
      this.getImages();
    },
    //打开对话框
    openDiglog() {
      this.dialogVisible = true;
      this.getImages();
      this.selectImgUrl = null;
      this.uploadUrl = null;
      this.perviewImg = "images";
    },
    //收藏和全部的切换
    toggleList() {
      this.reqParams.page = 1;
      this.getImages();
    },
    // 获取inage的数据

    async getImages() {
      const res = await this.$http.get("user/images", {
        params: this.reqParams,
      });
      console.log(res);
      this.images = res.data.data.results;
      this.total = res.data.data.total_count;
    },
  },
};
</script>

<style lang='less' scoped>
.my-image {
  display: inline-block;
  margin-right: 20px;
  .btn-img {
    width: 150px;
    height: 150px;
    border: 1px dashed #ddd;

    img {
      width: 100%;
      height: 100%;
      display: block;
    }
  }
}
.list-img {
  .img-item {
    width: 150px;
    height: 120px;
    display: inline-block;
    margin-right: 20px;
    position: relative;
    img {
      width: 100%;
      height: 100%;
    }
    &.selected::after {
      content: "";
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      background: rgba(0, 0, 0, 0.3) url(../assets/selected.png) no-repeat
        center / 50px auto;
    }
  }
}
::v-deep .el-dialog__body {
  padding: 0 20px;
}
</style>